<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of File
 *
 * @author Cata
 */
class File {
    public $Id;
    public $Name;
    public $HierarchyId;
    public $CreateDate;
    public $LastUpdateDate;
    public $Ip;
    public $Size;
    public $FileTypeId;
    
    public static function findById($id){
        require_once 'script.php';
        $sql = "SELECT * FROM file WHERE Id = " . $id;
        $query= mysql_query($sql);
        $res = mysql_fetch_array($query);
        $file = new File();
        $file->Id = $res['Id'];
        $file->Name = $res['Name'];
        $file->HierarchyId = $res['HierarchyId'];
        $file->CreateDate = $res['CreateDate'];
        $file->LastUpdateDate = $res['LastUpdateDate'];
        $file->Ip = $res['Ip'];
        $file->Size = $res['Size'];
        $file->FileTypeId = $res['FileTypeId'];
        return $file;      
    }
    
    public static function findByHierarchy($hierarchy){  // we get a file by its hierarchi id
        $sql = "SELECT * FROM file WHERE HierarchyId = " . $hierarchy;
        $query= mysql_query($sql);
        $res = mysql_fetch_array($query);
        $file = new File();
        $file->Id = $res['Id'];
        $file->Name = $res['Name'];
        $file->HierarchyId = $res['HierarchyId'];
        $file->CreateDate = $res['CreateDate'];
        $file->LastUpdateDate = $res['LastUpdateDate'];
        $file->Ip = $res['Ip'];
        $file->Size = $res['Size'];
        $file->FileTypeId = $res['FileTypeId'];
        return $file;   
    }
    
    public function save(){
        require_once 'script.php';
        
        if ($this->FileTypeId == 1){
            $this->Size = 0;
        }
        
        $sql = "INSERT INTO file SET
                    Name = '" . $this->Name . "',
                    HierarchyId = '" . $this->HierarchyId . "',
                    CreateDate = '" . $this->CreateDate . "',
                    LastUpdateDate = '" . $this->LastUpdateDate . "',
                    Ip = '" . $this->Ip . "',
                    Size = " . $this->Size . ",
                    FileTypeId = '" . $this->FileTypeId . "'";
        $query = mysql_query($sql);
    }
    
    public static function getCurrentFiles($hierarchy){ // we return the files info from one level
        $hierarchies = Hierarchy::getCurrentHierarchies($hierarchy); // we get all hierarchies of the current level
        $index = 0;
        foreach($hierarchies as $hierarchy){
            $files[$index] = File::findByHierarchy($hierarchy->Id); // we get the correspondin files info
            $index++;
        }
        return $files;
    }
    
    public function getExtension(){
        $sql = "SELECT Extension FROM filetype WHERE Id = " . $this->FileTypeId;
        $query = mysql_query($sql);
        $result = mysql_fetch_array($query);
        if ($result['Extension']) {
            $ext = "." . $result['Extension'];
        } else {
            $ext = null;
        }
        return $ext;
    }
    
    public function delete(){
        $sql = "DELETE FROM file WHERE Id = ". $this->Id;
        $query = mysql_query($sql);
    }
    
    public function deleteAllChildren($id){
        $files = Hierarchy::findAllChildren($id); // find all children of a file
        foreach($files as $file){
            $file->delete();
        }
    }
        
    
    public static function deleteALL($id){
        $file = File::findById($id);
        $hierarchy = Hierarchy::findById($file->HierarchyId);
        $file->deleteAllChildren($hierarchy->Id); // we delete all file info of the current file and all its children
        $hierarchy->deleteAll();       // we delete all file hierachy of the current file and all its children
    }
    
    public function getAllPossibleFolders(){
        $sql = "SELECT Id FROM filetype WHERE Name = 'folder'";
        $result = mysql_fetch_array(mysql_query($sql)); 
        $sql = 'SELECT f.Id as fileId FROM file f
                    JOIN hierarchy h ON h.Id = f.HierarchyId
                WHERE 
                    f.Id != ' . $this->Id . ' AND 
                    f.FileTypeId = ' . $result['Id'] . ' AND 
                    h.IdHash not like "%' . $this->HierarchyId . '%";';
        $result = mysql_query($sql);
        $index = 0;
        while($res = mysql_fetch_array($result)){
            $files[$index] = File::findById($res['fileId']);
            $index++;
        }
        return $files;
    }
    
    public function copy($destination, $currenthierrachy){
        $destinationFolder = File::findById($destination);
        $destinationHierarchy = Hierarchy::findById($destinationFolder->HierarchyId);
        $copySourceHierarchy = Hierarchy::findById($this->HierarchyId);
        $copySourceHierarchy->copyAll($destinationHierarchy);  
    }
}

?>
